//
//  EditPhotoMarkViewController.m
//  iBaby365v2
//
//  Created by Kirinrin on 14-8-13.
//  Copyright (c) 2014年 suplus. All rights reserved.
//

#import "EditPhotoMarkViewController.h"
#import "SNSMarkLabelView.h"
#import "UIAlertView+Kirinrin.h"

@interface EditPhotoMarkViewController ()
@property (weak, nonatomic) IBOutlet UITextView *textView;

@end

@implementation EditPhotoMarkViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if (self.data) {
        self.textView.text = self.data.contentText;
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:)name:UIKeyboardWillHideNotification object:nil];
    
    [self.textView becomeFirstResponder];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    
    
    [self.view endEditing:YES];
}

#pragma mark - Responding to keyboard events
- (void)keyboardWillShow:(NSNotification *)notification {
    //    NSLog(@"keyboardWillShow");
    
    NSDictionary *userInfo = [notification userInfo];
    
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    
    CGRect keyboardRect = [aValue CGRectValue];
    
    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    
    NSTimeInterval animationDuration;
    
    [animationDurationValue getValue:&animationDuration];
    
    UIEdgeInsets currentInsets = self.textView.contentInset;
    currentInsets.bottom = keyboardRect.size.height;
    self.textView.contentInset = currentInsets;

}


- (void)keyboardWillHide:(NSNotification *)notification {
    //    NSLog(@"keyboardWillHide");
    
    NSDictionary* userInfo = [notification userInfo];
    NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    
    CGRect keyboardRect = [aValue CGRectValue];
    
    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval animationDuration;
    [animationDurationValue getValue:&animationDuration];
    [UIView animateWithDuration:animationDuration animations:^{
        
        self.view.frame = CGRectMake(self.view.frame.origin.x, keyboardRect.origin.y-self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height);
    } completion:^(BOOL finished){
    }];
    
}
- (IBAction)saveMarkTextAction:(id)sender {
    NSString *textcontext = [self.textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    if (textcontext.length == 0) {
        [UIAlertView alertWithTitle:@"提示" message:@"请填写内容"];
        return;
    }
        
    if (self.data){
        self.data.contentText = textcontext;
        [self.delegate didFinishEditMark:self.data];
    }else{
        SNSMarkModel *result = [[SNSMarkModel alloc]init];
        result.pos = CGPointMake(100, 100);
        result.contentText = textcontext;
         [self.delegate didFinishAddMark:result];
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
